package modelLayer;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * PersonContainer is a class that contain all the Person objects
 * @author group 6 - Stefan Patatu, Stan Cosmin Madalin, Monika Vyšniauskaitė, Larowski Michal, Njogu Alex, Cristian Moldovan
 */
public class PersonContainer {
    private static PersonContainer instance = null;
    private ArrayList<Person> people;
    private static int incrementedID = 0;
    
    /**
     * Constructor af an ArrayList of type Person.
     * @param people The  array list that holds all the Person objects.
     */
    private PersonContainer() {
        people = new ArrayList<>();
    }
    
    /**
     * Singleton pattern which ensure that only one instance of a class is created.
     * @param instance The instance of the PersonContainer class.
     */
    public static PersonContainer getInstance() {
        if (instance == null) {
            instance = new PersonContainer();
        }
        return instance;
    }

    /**
     * Add a given Person object in the people ArrayList.
     * @param newPerson The new Person object.
     */
    public void addPerson(Person newPerson) {
        incrementedID++;
        newPerson.setID(incrementedID);
        people.add(newPerson);
    }
    
    /**
     * Get a Person object by a given ID.
     * @param ID The ID of a person.
     */
    public Person getPerson(int ID) {
        for (Iterator<Person> i = people.iterator(); i.hasNext();) {
            Person person = i.next();
            if (person.getID() == ID) {
                return person;
            }
        }
        return null;
    }
    
    /**
     * Update the Person object information such as name, address, postal code, city, phone.
     * @param ID the ID of the soon to be updated Person object.
     * @param name The name of the person.
     * @param address The address of the person.
     * @param postalCode The postal code of the person.
     * @param city The city of the person.
     * @param phone The phone number of the person.
     */
    public void updatePerson(int ID, String name, String address, String postalCode, String city, String phone) {
        for (Iterator<Person> i = people.iterator(); i.hasNext();) {
            Person person = i.next();
            if (person.getID() == ID) {
                person.setName(name);
                person.setAddress(address);
                person.setPostalCode(postalCode);
                person.setCity(city);
                person.setPhone(phone);
            }
        }
    }
    
    /**
     * Delete a Person object from the people ArryList.
     * @param ID The ID of the Person object to be deleted.
     */
    public void deletePerson(int ID) {
        for (Iterator<Person> i = people.iterator(); i.hasNext();) {
            Person person = i.next();
            if (person.getID() == ID) {
                i.remove();
            }
        }
    }
}
